<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="start" />
	<meta name="description" content="start" />
	<!-- 网页标签标题 -->
	<title>start</title>
	<link rel="shortcut icon" href="/img/docsite.ico"/>
	<link rel="stylesheet" href="/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/en-us/index.html"><img class="logo" src="/img/txlcn.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">中</span><div class="header-menu"><img class="header-menu-toggle" src="/img/system/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/en-us/index.html" target="_self">Home</a></li><li class="menu-item menu-item-normal"><a href="https://bbs.txlcn.org/viewtopic.php?id=42" target="_self">News</a></li><li class="menu-item menu-item-normal"><a href="https://bbs.txlcn.org/" target="_self">Forum</a></li><li class="menu-item menu-item-normal"><a href="/en-us/docs/preface.html" target="_self">Docs</a></li><li class="menu-item menu-item-normal"><a href="https://github.com/codingapi/tx-lcn/releases" target="_blank">Download</a></li><li class="menu-item menu-item-normal"><a href="/en-us/sponsor/index.html" target="_self">Sponsor</a></li></ul></div></div></header><div class="bar"><div class="bar-body"><img src="/img/system/docs.png" class="front-img"/><span>Documents</span><img src="/img/system/docs.png" class="back-img"/></div></div><section class="content-section"><div class="sidemenu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>User Guide</span><ul><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/preface.html" target="_self">Preface</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/background.html" target="_self">Background</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/start.html" target="_self">Quick Start</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/dependencies.html" target="_self">Dependencies</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Demos<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/demo/env.html" target="_self">DTX Demo</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/demo/dubbo.html" target="_self">Dubbo of TC</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/demo/springcloud.html" target="_self">Springcloud of TC</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>In-depth TXLCN<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/principle/control.html" target="_self">Explain</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/principle/lcn.html" target="_self">LCN Transaction Mode</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/principle/tcc.html" target="_self">TCC Transaction Mode</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/principle/txc.html" target="_self">TXC Transaction Mode</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Settings<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/setting/client.html" target="_self">TC Settigns</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/setting/manager.html" target="_self">TM Settings</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/setting/distributed.html" target="_self">LoadBalance and TM Cluster</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Extensions<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/expansion/transaction.html" target="_self">Transaction Mode</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/expansion/message.html" target="_self">TXMSG&#x27;s Protocol</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/expansion/rpc.html" target="_self">TXMSG</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/communication.html" target="_self">Command of TXMSG</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/manageradmin.html" target="_self">TM guide</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/debug.html" target="_self">When troubles</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/test.html" target="_self">Test report</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/developer.html" target="_self">Developers</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/en-us/docs/fqa.html" target="_self">FQA</a></li></ul></li></ul></div><div class="doc-content markdown-body"><h1>快速开始</h1>
<h2>说明</h2>
<p>TX-LCN 主要有两个模块，Tx-Client(TC) Tx-Manager(TM). TC作为微服务下的依赖，TM是独立的服务。<br>
本教程带领大家了解框架的基本步骤，详细配置可参考 <a href="demo/dubbo.md">dubbo</a> <a href="demo/springcloud.md">springcloud</a></p>
<h2>TM配置与启动</h2>
<h3>TM的准备环境</h3>
<p>TM需要依赖的服务有
JDK1.8+,Mysql5.6+,Redis3.2+,Git,Maven</p>
<p>初始化TM Mysql数据库
创建数据库名称为:tx-manager</p>
<pre><code class="language-sql"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> <span class="hljs-string">`t_tx_exception`</span>;
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">`t_tx_exception`</span>  (
  <span class="hljs-string">`id`</span> <span class="hljs-built_in">bigint</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> AUTO_INCREMENT,
  <span class="hljs-string">`group_id`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">32</span>) <span class="hljs-built_in">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8mb4 <span class="hljs-keyword">COLLATE</span> utf8mb4_general_ci <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">NULL</span>,
  <span class="hljs-string">`unit_id`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">32</span>) <span class="hljs-built_in">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8mb4 <span class="hljs-keyword">COLLATE</span> utf8mb4_general_ci <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">NULL</span>,
  <span class="hljs-string">`mod_id`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">32</span>) <span class="hljs-built_in">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8mb4 <span class="hljs-keyword">COLLATE</span> utf8mb4_general_ci <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">NULL</span>,
  <span class="hljs-string">`transaction_state`</span> <span class="hljs-built_in">tinyint</span>(<span class="hljs-number">4</span>) <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">NULL</span>,
  <span class="hljs-string">`registrar`</span> <span class="hljs-built_in">tinyint</span>(<span class="hljs-number">4</span>) <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'-1 未知 0 Manager 通知事务失败， 1 client询问事务状态失败2 事务发起方关闭事务组失败'</span>,
  <span class="hljs-string">`ex_state`</span> <span class="hljs-built_in">tinyint</span>(<span class="hljs-number">4</span>) <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'0 待处理 1已处理'</span>,
  <span class="hljs-string">`create_time`</span> datetime(<span class="hljs-number">0</span>) <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-literal">NULL</span>,
  PRIMARY <span class="hljs-keyword">KEY</span> (<span class="hljs-string">`id`</span>) <span class="hljs-keyword">USING</span> BTREE
) <span class="hljs-keyword">ENGINE</span> = <span class="hljs-keyword">InnoDB</span> AUTO_INCREMENT = <span class="hljs-number">967</span> <span class="hljs-built_in">CHARACTER</span> <span class="hljs-keyword">SET</span> = utf8mb4 <span class="hljs-keyword">COLLATE</span> = utf8mb4_general_ci ROW_FORMAT = Dynamic;

<span class="hljs-keyword">SET</span> FOREIGN_KEY_CHECKS = <span class="hljs-number">1</span>;

</code></pre>
<h3>TM下载与配置</h3>
<p><code>git clone https://github.com/codingapi/tx-lcn.git</code></p>
<p>修改配置信息(txlcn-tm\src\main\resources\application.properties)</p>
<pre><code>spring.application.name=tx-manager
server.port=7970

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true

#tx-lcn.logger.enabled=true
# TxManager Host Ip
#tx-lcn.manager.host=127.0.0.1
# TxClient连接请求端口
#tx-lcn.manager.port=8070
# 心跳检测时间(ms)
#tx-lcn.manager.heart-time=15000
# 分布式事务执行总时间
#tx-lcn.manager.dtx-time=30000
#参数延迟删除时间单位ms
#tx-lcn.message.netty.attr-delay-time=10000
#tx-lcn.manager.concurrent-level=128
# 开启日志
#tx-lcn.logger.enabled=true
#logging.level.com.codingapi=debug
#redisIp
#spring.redis.host=127.0.0.1
#redis\u7AEF\u53E3
#spring.redis.port=6379
#redis\u5BC6\u7801
#spring.redis.password=


</code></pre>
<ul>
<li><code>#</code>给出信息都是默认值</li>
</ul>
<p>关于详细配置说明见 <a href="setting/manager.md">manager</a></p>
<h3>TM编译与启动</h3>
<p>编译<br>
进入到txlcn-tm路径下。 执行 <code>mvn clean package '-Dmaven.test.skip=true'</code><br>
启动<br>
进入target文件夹下。执行 <code>java -jar txlcn-tm-5.0.0.RELEASE.jar</code></p>
<h2>TC微服务模块</h2>
<p>微服务示例架构</p>
<p><img src="../../img/quick_arch.png" alt="arch"></p>
<ul>
<li>服务A作为DTX发起方，远程调用服务B</li>
</ul>
<h3>TC引入pom依赖</h3>
<pre><code class="language-xml">        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.codingapi.txlcn<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>txlcn-tc<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.0.0.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.codingapi.txlcn<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>txlcn-txmsg-netty<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.0.0.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<h3>TC开启分布式事务注解</h3>
<p>在主类上使用<code>@EnableDistributedTransaction</code></p>
<pre><code class="language-java"><span class="hljs-meta">@SpringBootApplication</span>
<span class="hljs-meta">@EnableDistributedTransaction</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoAApplication</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        SpringApplication.run(DemoDubboClientApplication.class, args);
    }

}
</code></pre>
<h3>TC微服务A业务方法配置</h3>
<pre><code class="language-java"><span class="hljs-meta">@Service</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServiceA</span> </span>{
    
    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> ValueDao valueDao; <span class="hljs-comment">//本地db操作</span>
    
    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> ServiceB serviceB;<span class="hljs-comment">//远程B模块业务</span>
    
    <span class="hljs-meta">@LcnTransaction</span> <span class="hljs-comment">//分布式事务注解</span>
    <span class="hljs-meta">@Transactional</span> <span class="hljs-comment">//本地事务注解</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">execute</span><span class="hljs-params">(String value)</span> <span class="hljs-keyword">throws</span> BusinessException </span>{
        <span class="hljs-comment">// step1. call remote service B</span>
        String result = serviceB.rpc(value);  <span class="hljs-comment">// (1)</span>
        <span class="hljs-comment">// step2. local store operate. DTX commit if save success, rollback if not.</span>
        valueDao.save(value);  <span class="hljs-comment">// (2)</span>
        valueDao.saveBackup(value);  <span class="hljs-comment">// (3)</span>
        <span class="hljs-keyword">return</span> result + <span class="hljs-string">" &gt; "</span> + <span class="hljs-string">"ok-A"</span>;
    }
}
</code></pre>
<h3>TC微服务B业务方法配置</h3>
<pre><code class="language-java"><span class="hljs-meta">@Service</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServiceB</span> </span>{
    
    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> ValueDao valueDao; <span class="hljs-comment">//本地db操作</span>
    
    <span class="hljs-meta">@LcnTransaction</span> <span class="hljs-comment">//分布式事务注解</span>
    <span class="hljs-meta">@Transactional</span>  <span class="hljs-comment">//本地事务注解</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">rpc</span><span class="hljs-params">(String value)</span> <span class="hljs-keyword">throws</span> BusinessException </span>{
        valueDao.save(value);  <span class="hljs-comment">// (4)</span>
        valueDao.saveBackup(value);  <span class="hljs-comment">// (5)</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"ok-B"</span>;
    }
}
</code></pre>
<h3>TC配置信息说明</h3>
<pre><code># 默认之配置为TM的本机默认端口
tx-lcn.client.manager-address=127.0.0.1:8070 
</code></pre>
</div></section><footer class="footer-container"><div class="footer-body"><img src="/img/txlcn.png"/><div class="cols-container"><div class="col col-12"><h3>Vision</h3><p>We will devote ourselves to creating a fast, efficient and concurrent distributed transaction solution.</p></div><div class="col col-6"><dl><dt>Navigation</dt><dd><a href="/zh-cn/docs/txlcn-arch.html" target="_self">TX-LCN Solution</a></dd><dd><a href="/zh-cn/docs/start.html" target="_self">Get Started</a></dd><dd><a href="/zh-cn/docs/txlcn-mod.html" target="_self">Develop Plan</a></dd><dd><a href="https://bbs.txlcn.org/viewforum.php?id=8" target="_self">Report Bug</a></dd></dl></div><div class="col col-6"><dl><dt>Friendship Link</dt><dd><a href="https://www.codingapi.com/" target="_blank">CodingApi</a></dd></dl></div></div><div class="copyright"><span>Copyright © 2018 CodingApi</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '';
  </script>
	<script src="/build/documentation.js"></script>
</body>
</html>